package cn.peach.order.service.impl;

import cn.peach.basic.exception.MyException;
import cn.peach.basic.service.impl.BaseServiceImpl;
import cn.peach.basic.util.LoginContextUtil;
import cn.peach.order.domain.OrderAddress;
import cn.peach.order.domain.OrderPet;
import cn.peach.order.domain.OrderPetDetail;
import cn.peach.order.dto.SaveOrderDto;
import cn.peach.order.service.IOrderAddressService;
import cn.peach.order.service.IOrderPetDetailService;
import cn.peach.order.service.IOrderPetService;
import cn.peach.pet.domain.Pet;
import cn.peach.pet.service.IPetService;
import cn.peach.user.domain.Logininfo;
import cn.peach.user.domain.UserAddress;
import cn.peach.user.mapper.UserAddressMapper;
import org.apache.commons.lang3.time.DateUtils;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

@Service
public class OrderPetServiceImpl extends BaseServiceImpl<OrderPet> implements IOrderPetService {

    @Autowired
    private IPetService petService;

    @Autowired
    private IOrderPetDetailService orderPetDetailService;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private IOrderAddressService orderAddressService;

    @Override
    public void placeOrder(SaveOrderDto dto) {
        /*
        * 参数的非空校验
        */
        Long addressId = dto.getAddressId();
        Integer payType = dto.getPayType();
        Long petId = dto.getPetId();
        if (null == addressId) {
            throw new MyException("达咩达的地址");
        }
        if (null == payType) {
            throw new MyException("达咩达的支付类型");
        }
        if (null == petId) {
            throw new MyException("达咩达的宠物选项");
        }
        /*查询宠物有没有上架以及宠物类型和店铺id*/
        Pet pet = petService.selectOnlineById(petId);
        if (null == pet) {
            throw new MyException("达咩达的宠物id");
        }
        /*查询用户住址 支付方式*/
        UserAddress userAddress = userAddressMapper.selectByidI(addressId);
        if (null == userAddress) {
            throw new MyException("达咩达的地址");
        }
        /**
         * 用户订单事项
         */
        // 订单号
        String orderNo = UUID.randomUUID().toString().replace("-", "");
        // 计算超时时间，并保存订单
        Date dateNow = new Date();
        Date minutes = DateUtils.addMinutes(dateNow, 30);
        // 保存订单
        OrderPet orderPet = new OrderPet();
        /*订单中的各种信息数据*/
        orderPet.setCreateTime(dateNow);
        orderPet.setDigest("购买" + pet.getName() + "下单");
        orderPet.setOrderSn(orderNo);
        orderPet.setPayType(payType);
        orderPet.setTotalCount(1);
        orderPet.setState(OrderPet.STATE_WAIT_PAY);
        orderPet.setPayExpireTime(minutes);
        orderPet.setTotalPrice(pet.getSaleprice().add(new BigDecimal("10")));
        Logininfo loginContext = LoginContextUtil.getLoginContext();
        if (loginContext == null) {
            throw new MyException("亲，请登录");
        }
        orderPet.setUserId(loginContext.getId());
        // 保存订单 super.insert(orderPet);
        insert(orderPet);

        /*后台保存订单明细事项*/
        OrderPetDetail detail = new OrderPetDetail();
        detail.setCount(1);
        detail.setCreatetime(dateNow);
        detail.setName(pet.getName());
        detail.setOrderId(orderPet.getId());
        detail.setOrderSn(orderNo);
        detail.setPetId(pet.getId());
        detail.setPrice(pet.getSaleprice());
        detail.setResources(pet.getResources());
        detail.setShopId(pet.getShop().getId());
        detail.setShopName(pet.getShop().getName());
        // 保存详情
        orderPetDetailService.insert(detail);
        /* 保存的订单地址*/
        OrderAddress address = new OrderAddress();
        // 属性拷贝
        BeanUtils.copyProperties(userAddress, address);
        address.setOrderSn(orderNo);
        address.setOrderId(orderPet.getId());
        address.setCreateTime(dateNow);
        // 保存地址
        orderAddressService.insert(address);
        /* 用户购买过后 后台库存数量需要减一 考虑到并发问题 需要防止超卖 加上锁*/
        synchronized (this){
            if (pet.getStore_number() == null || pet.getStore_number().intValue() == 0) {
                throw new MyException("商品已经被别人购买了");
            }
            pet.setStore_number(pet.getStore_number() - 1);
        }
        petService.update(pet);
    }
}
